package xc.src.demo02;

import org.apache.commons.io.FileUtils;
import xc.src.demo.WebDownloader;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

//线程实现方式3:实现callable接口
public class TestCallable implements Callable<Boolean> {


    private String url;//网络图片地址
    private String name;//保存的文件名

    public TestCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    //下载图片线程的执行体
    @Override
    public Boolean call() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名为:" + name);
        return true;
    }

    //主线程
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建三个线程
        TestCallable t1 = new TestCallable("https://qn-sfe.yuketang.cn/o_1c9oq0sec149j1g47ccl10urj0tg.png", "1,jpg");
        TestCallable t2 = new TestCallable("https://qn-sfe.yuketang.cn/o_1c9oq0sec149j1g47ccl10urj0tg.png", "2,jpg");
        TestCallable t3 = new TestCallable("https://qn-sfe.yuketang.cn/o_1c9oq0sec149j1g47ccl10urj0tg.png", "3,jpg");

        //创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(3);

        //提交执行
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        //获取结果
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();

        System.out.println(rs1);
        System.out.println(rs2);
        System.out.println(rs3);

        //关闭服务
        ser.shutdown();

    }
}